package com.wtf.generator.controller;

import com.wtf.generator.common.sys.R;
import com.wtf.generator.common.utils.PageUtils;
import com.wtf.generator.model.dto.CodeDTO;
import com.wtf.generator.model.dto.TableDTO;
import com.wtf.generator.service.DbTableSerivce;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
/**
 * date: 2022/6/18 10:59
 * @author linhuanyou
 * @email 496558736@qq.com
 * @version 1.0
 */
@Api(tags = "数据库表操作")
@RestController
@RequestMapping("/table")
public class DbTableController {

    @Autowired
    private DbTableSerivce dbTableSerivce;

    @ApiOperation(value = "获取数据库表")
    @GetMapping("/page")
    public R page(TableDTO tableDTO) throws Exception {
        PageUtils pageUtils = dbTableSerivce.page(tableDTO);
        return R.ok(pageUtils);
    }

    @ApiOperation(value = "表代码生成")
    @GetMapping("/code")
    public void generatorCode(@Validated CodeDTO codeDTO, HttpServletResponse response) throws Exception {
        byte[] data = dbTableSerivce.generatorCode(codeDTO);
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"wtf.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
}
